﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Base;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmReturnVisitAdd : FormBase
    {
        private int iCustomerID = -1;
        private SortedList<string, int> listEmployee;

        public frmReturnVisitAdd()
        {
            InitializeComponent();
            customSearch1.onSerchCustom += this.SearchCustomer;
            listEmployee = new SortedList<string, int>();
        }

        private void SearchCustomer(DataRow dr)
        {
            iCustomerID = Convert.ToInt32(dr[DBEntityCustomInfo.FieldName.CUSTOMERID]);
            lblName.Text = dr[DBEntityCustomInfo.FieldName.CNAME].ToString();
            lblNickName.Text = dr[DBEntityCustomInfo.FieldName.CNICKNAME].ToString();
            lblPhone.Text = dr[DBEntityCustomInfo.FieldName.CMOBILEPHONE].ToString();
            lblLastVisit.Text = dr[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE].ToString();
        }

        public ControlClassModeEnum Mode { get; set; }

        public DBEntityCustomerReturnVisit CustomerReturnVisitInfo { get; set; }

        private void frmReturnVisitAdd_Shown(object sender, EventArgs e)
        {
            InitEmployee();
            diReturnVisit.Value = DateTime.Now;
            if (Mode == ControlClassModeEnum.emEdit)
            {
                DBEntityCustomInfo customerInfo = InitCustomerInfo(CustomerReturnVisitInfo.CUSTOMERID);
                iCustomerID = CustomerReturnVisitInfo.CUSTOMERID;
                lblName.Text = customerInfo.CNAME;
                lblNickName.Text = customerInfo.CNICKNAME;
                lblPhone.Text = customerInfo.CMOBILEPHONE;
                lblLastVisit.Text = customerInfo.CLASTREGISTERDATE.ToString();

                diReturnVisit.Value = CustomerReturnVisitInfo.CRETURNVISITTIME;
                txtMemo.Text = CustomerReturnVisitInfo.CMEMO;

                int iIndex = listEmployee.Values.IndexOf(CustomerReturnVisitInfo.CEMPLOYEEID);
                string sEmployeeName = listEmployee.Keys[iIndex];
                cbEmployee.Text = sEmployeeName;
            }
        }

        private void InitEmployee()
        {
            string error = string.Empty;
            DataSet dsEmployee = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryEmployeeState(DBEntityEmployee.ENUM_ESTATE.F0E, out dsEmployee, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取经手人失败\r\n" + error);
                return;
            }

            foreach (DataRow dr in dsEmployee.Tables[0].Rows)
            {
                int iEmployeeID = Convert.ToInt32(dr[DBEntityEmployee.FieldName.EMPLOYEEID]);
                string sName = dr[DBEntityEmployee.FieldName.ENAME].ToString();
                if (!listEmployee.ContainsKey(sName))
                {
                    listEmployee.Add(sName, iEmployeeID);
                }
                cbEmployee.Items.Add(sName);
            }
            if (cbEmployee.Items.Count > 0)
                cbEmployee.SelectedIndex = 0;
        }

        private DBEntityCustomInfo InitCustomerInfo(int customerID)
        {
            string error = string.Empty;
            DataSet dsCustomer = new DataSet();
            DBEntityCustomInfo customerInfo = null;
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerID(customerID, out dsCustomer, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员信息失败\r\n" + error);
                return customerInfo;
            }
            if (dsCustomer != null && dsCustomer.Tables.Count > 0 && dsCustomer.Tables[0].Rows.Count > 0)
            {
                DataRow dr = dsCustomer.Tables[0].Rows[0];
                customerInfo = new DBEntityCustomInfo(dr);
            }
            return customerInfo;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool ValidateFormInput()
        {
            string error = "";
            if (iCustomerID < 0)
            {
                MsgBox.MsgInfo("请选择会员。");
                return false;
            }
            if (diReturnVisit.Text == string.Empty)
            {
                error = "请输入回访日期。";
                MsgBox.MsgInfo(error);
                SetErrorControl(diReturnVisit, error);
                return false;
            }
            if (cbEmployee.SelectedIndex < 0)
            {
                error = "请选择经手人。";
                MsgBox.MsgInfo(error);
                SetErrorControl(cbEmployee, error);
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateFormInput() == false)
                return;

            int iEmployeeID = -1;
            if (listEmployee.ContainsKey(cbEmployee.SelectedItem.ToString()))
            {
                iEmployeeID = listEmployee[cbEmployee.SelectedItem.ToString()];
            }

            string error = string.Empty;
            if (Mode == ControlClassModeEnum.emInsert)
            {
                DBEntityCustomerReturnVisit visitInfo = new DBEntityCustomerReturnVisit();
                visitInfo.CEMPLOYEEID = iEmployeeID;
                visitInfo.CMEMO = txtMemo.Text;
                visitInfo.CRETURNVISITTIME = diReturnVisit.Value;
                visitInfo.CUSTOMERID = iCustomerID;

                bool bFlag = BussinessProxyManager.Instance.InsertCustomerReturnVisit(visitInfo, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("新增回访信息失败\r\n" + error);
                    return;
                }
            }
            else if (Mode == ControlClassModeEnum.emEdit)
            {
                CustomerReturnVisitInfo.CEMPLOYEEID = iEmployeeID;
                CustomerReturnVisitInfo.CMEMO = txtMemo.Text;
                CustomerReturnVisitInfo.CRETURNVISITTIME = diReturnVisit.Value;
                CustomerReturnVisitInfo.CUSTOMERID = iCustomerID;

                bool bFlag = BussinessProxyManager.Instance.UpdateCustomerReturnVisit(CustomerReturnVisitInfo, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("修改回访信息失败\r\n" + error);
                    return;
                }
            }

            MsgBox.MsgInfo("保存成功");
            this.DialogResult = DialogResult.OK;
        }
    }
}
